import type { BasicTableProps } from "../types/table";
import TableHeader from "../components/TableHeader";
import { isString } from "@/utils/is";
import { useMemo } from "react";
import React from "react";

export function useTableHeader(propsRef: BasicTableProps, slots: SlotsComponent) {
	const getHeaderProps = useMemo(() => {
		const { title, showTableSetting } = propsRef;

		const hideTitle = !slots.tableTitle && !title && !slots.toolbar && !showTableSetting;
		if (hideTitle && !isString(title)) return {};
		return {
			title: hideTitle
				? null
				: () =>
						React.createElement(TableHeader, {
							title: propsRef.title as string,
							titleHelpMessage: propsRef.titleHelpMessage!,
							showTableSetting: propsRef.showTableSetting!,
							tableSetting: propsRef.tableSetting!,
							slots
						})
		};
	}, [propsRef.title, propsRef.showTableSetting]);

	return { getHeaderProps };
}
